/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import core.Matrix;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alexey Chirkov
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length != 3) {
            System.out.println("Не заданны входные параметры");
        }
        Matrix mat1 = null;
        Matrix mat2 = null;
        try {
            mat1 = Matrix.readMatrix(args[0]);
            mat2 = Matrix.readMatrix(args[1]);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return;
        }

        Socket connect = null;
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        Matrix result = new Matrix();
        try {
            connect = new Socket("192.168.173.135", 8086);
            ois = new ObjectInputStream(connect.getInputStream());
            oos = new ObjectOutputStream(connect.getOutputStream());
        } catch (UnknownHostException ex) {
            System.out.println("Соединение с сервером не установленно");
            return;
        } catch (IOException ex) {
            System.out.println("Ошибка открытия потока чтения/записи ");
            ex.printStackTrace();
            return;
        }

        try {
            mat1.sendMatrix(oos);
            mat2.sendMatrix(oos);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        Object inObject = null;
        try {

            inObject = ois.readObject();
            result = (Matrix) inObject;

        } catch (Exception ex) {
            result=null;
        }

        try {
            oos.close();
            ois.close();
            connect.close();
        } catch (Exception ex) {
        }

        try {
            if (result != null) {
                Matrix.writeMatrix(result, args[2]);
System.out.print(result);
            } else {
                System.out.print(inObject);
                FileWriter fos = new FileWriter(args[2]);
                fos.write((String) inObject);
                fos.flush();
                fos.close();
            }
        } catch (Exception ex) {
        }


    }
}
